package com.heima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.mapper.PartnerMapper;
import com.heima.pojo.entity.Partner;
import com.heima.pojo.vo.PartnerByIdVo;
import com.heima.pojo.vo.PartnerPageVo;
import com.heima.service.PartnerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class PartnerServiceImpl implements PartnerService {
    @Autowired
    private PartnerMapper partnerMapper;

    /**
     * 合作商分页搜索
     * @param pageIndex
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public PartnerPageVo pagePartner(Integer pageIndex, Integer pageSize, String name) {
        PageHelper.startPage(pageIndex,pageSize);
        //获取合作商信息
       List<Partner> partners = partnerMapper.getPartnerList(name);
       //强转
        Page<Partner> page = (Page<Partner>) partners;
        //封装返回值
        PartnerPageVo partnerPageVo = new PartnerPageVo();
        partnerPageVo.setPageSize(pageSize);
        partnerPageVo.setPageIndex(pageIndex);
        partnerPageVo.setTotalCount(page.getTotal());
        partnerPageVo.setTotalPage(page.getPages());
        partnerPageVo.setCurrentPageRecords(partners);
        //返回查询结果
        return partnerPageVo;
    }

    /**
     * 新增合作商
     * @param partner
     * @return
     */
    @Override
    public Boolean save(Partner partner) {
        partner.setCreateTime(LocalDateTime.now());
        partner.setUpdateTime(LocalDateTime.now());
       Boolean result = partnerMapper.save(partner);
        return result;
    }

    /**
     * 通过id获取合作商详情
     * @param id
     * @return
     */
    @Override
    public PartnerByIdVo getPartById(String id) {
       Partner partner = partnerMapper.getPartById(id);
        PartnerByIdVo partnerByIdVo = new PartnerByIdVo();
        BeanUtils.copyProperties(partner,partnerByIdVo);
        return partnerByIdVo;
    }

    /**
     * 修改合作商信息
     * @param id
     * @param partner
     * @return
     */
    @Override
    public Boolean update(String id, Partner partner)throws NullPointerException {
        //判断合作商是否存在
        Partner partner1= partnerMapper.getPartById(id);
        if (partner1 == null){
            throw new NullPointerException("合作商不存在");
        }
        //修改合作商信息
        partner.setUpdateTime(LocalDateTime.now());
        Boolean result = partnerMapper.update(id,partner);
        return result;
    }

    /**
     * 删除合作商
     * @param id
     * @return
     */
    @Override
    public Boolean delete(String id) {
        // 检查合作商是否存在
        Partner partner = partnerMapper.getPartById(id);
        if (partner == null) {
            throw new RuntimeException("合作商不存在");
        }
        Boolean result = partnerMapper.delete(id);
        return result;
    }

    /**
     * 获取合作商数量
     * @return
     */
    @Override
    public Integer getPartnerCount() {
        List<Partner> partners = partnerMapper.selectList(new LambdaQueryWrapper<>());
        int count = partners.size();
        return count;
    }
}




















